import { defineStore } from "pinia";
import { ref } from "vue";
import type { Ref } from "vue";
import { ResponseType } from "@/types/commonTypes/commonTypes.ts";
import type { LoginParams, Patient, UserInfo, UserMessage } from "@/types/userTypes/userTypes.ts";
import { getUserInfo, removeUserInfo, setUserInfo } from "@/utils/localStorage.ts";
import { addPatientAPI, deletePatientAPI, fetchPatientListAPI, fetchUserMessageAPI, findPatientAPI, mobileLoginAPI, passwordLoginAPI, sendCodeAPI, updatePatientAPI } from "@/api/userApi/userApi.ts";
import UserStoreInterface from "@/pinia/modules/userStore/interface.ts";

const useUserStore = defineStore("userStore", (): UserStoreInterface => {

   const userInfo: Ref<UserInfo> = ref<UserInfo>(getUserInfo<UserInfo>() || {} as UserInfo);

   const userMessage: Ref<UserMessage> = ref<UserMessage>({} as UserMessage);

   const patientList: Ref<Patient[]> = ref<Patient[]>([]);

   const patientInfo: Ref<Patient> = ref<Patient>(<Patient>{});

   const passwordLogin = async (data: LoginParams): Promise<string> => {
      const response: ResponseType<UserInfo> = await passwordLoginAPI(data);
      userInfo.value = response.data;
      setUserInfo<UserInfo>(response.data);
      return "登录成功";
   };

   const mobileLogin = async (data: LoginParams): Promise<string> => {
      const response: ResponseType<UserInfo> = await mobileLoginAPI(data);
      userInfo.value = response.data;
      setUserInfo<UserInfo>(response.data);
      return "登录成功";
   };

   const logout = async (): Promise<string> => {
      removeUserInfo();
      userInfo.value = {} as UserInfo;
      userMessage.value = {} as UserMessage;
      return "退出成功";
   };

   const sendCode = async (mobile: string, type: string): Promise<{ code: string, message: string }> => {
      const response: ResponseType<{ code: string }> = await sendCodeAPI(mobile, type);
      return {
         code: response.data.code,
         message: "获取验证码成功",
      };
   };

   const fetchUserMessage = async (): Promise<void> => {
      const { data }: { data: UserMessage } = await fetchUserMessageAPI();
      userMessage.value = data;
   };

   const fetchPatientList = async (): Promise<void> => {
      const { data }: { data: Patient[] } = await fetchPatientListAPI();
      patientList.value = data;
   };

   const deletePatient = async (id: string): Promise<string> => {
      await deletePatientAPI(id);
      patientList.value = patientList.value.filter((item: Patient): boolean => item.id !== id);
      return "删除患者成功";
   };

   const addPatient = async (data: Patient): Promise<string> => {
      await addPatientAPI(data);
      return "添加患者成功";
   };

   const updatePatient = async (data: Patient): Promise<string> => {
      await updatePatientAPI(data);
      return "编辑患者成功";
   };

   const findPatient = async (id: string): Promise<void> => {
      const { data }: { data: Patient } = await findPatientAPI(id);
      patientInfo.value = data;
   };

   return {
      userInfo,
      userMessage,
      patientList,
      patientInfo,
      passwordLogin,
      mobileLogin,
      sendCode,
      logout,
      fetchUserMessage,
      fetchPatientList,
      deletePatient,
      addPatient,
      updatePatient,
      findPatient,
   };
});


export default useUserStore;
